00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef PTI23_PARSER_HPP_
00014 #define PTI23_PARSER_HPP_
00015
00016
00017 #include <boost/algorithm/string/split.hpp>
00018 #include <boost/algorithm/string/classification.hpp>
00019 #include <vector>
00020 #include <map>
00021 #include <cstdio>
00022 #include <cstdlib>
00023
00024 #include "gridpack/utilities/exception.hpp"
00025 #include "gridpack/timer/coarse_timer.hpp"
00026 #include "gridpack/parser/dictionary.hpp"
00027 #include "gridpack/network/base_network.hpp"
00028 #include "gridpack/parser/base_parser.hpp"
00029 #include "gridpack/parser/base_pti_parser.hpp"
00030 #include "gridpack/parser/hash_distr.hpp"
00031
00032 #define TERM_CHAR '0'
00033
00034
00035 namespace gridpack {
00036 namespace parser {
00037
00038
00039 template <class _network>
00040 class PTI23_parser : public BasePTIParser<_network>
00041 {
00042 public:
00043
00044
00045
00046
00047
00048
00049 PTI23_parser(boost::shared_ptr<_network> network)
00050 : p_network(network)
00051 {
00052 this->setNetwork(network);
00053 p_network_data = network->getNetworkData();
00054 }
00055
00056
00057
00058
00059 virtual ~PTI23_parser()
00060 {
00061 p_busData.clear();
00062 p_branchData.clear();
00063 }
00064
00065
00066
00067
00068
00069 void parse(const std::string &fileName)
00070 {
00071 p_timer = gridpack::utility::CoarseTimer::instance();
00072 p_timer->configTimer(false);
00073 int t_total = p_timer->createCategory("Parser:Total Elapsed Time");
00074 p_timer->start(t_total);
00075 gridpack::utility::StringUtils util;
00076 std::string tmpstr = fileName;
00077 util.trim(tmpstr);
00078 std::string ext = this->getExtension(tmpstr);
00079 if (ext == "raw") {
00080 getCase(tmpstr);
00081
00082 this->createNetwork(p_busData,p_branchData);
00083 } else if (ext == "dyr") {
00084 this->getDS(tmpstr);
00085 }
00086 p_timer->stop(t_total);
00087 p_timer->configTimer(true);
00088 }
00089
00090 protected:
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 void getCase(const std::string & fileName)
00102 {
00103
00104 int t_case = p_timer->createCategory("Parser:getCase");
00105 p_timer->start(t_case);
00106 p_busData.clear();
00107 p_branchData.clear();
00108 p_busMap.clear();
00109
00110 int me(p_network->communicator().rank());
00111
00112 p_case_id = 0;
00113 p_case_sbase = 0.0;
00114 if (me == 0) {
00115 std::ifstream input;
00116 input.open(fileName.c_str());
00117 if (!input.is_open()) {
00118 char buf[512];
00119 sprintf(buf,"Failed to open network configuration file: %s\n\n",
00120 fileName.c_str());
00121 throw gridpack::Exception(buf);
00122 }
00123
00124 find_case(input);
00125 find_buses(input);
00126
00127
00128
00129 bool parsed = true;
00130 std::string oldline;
00131 find_generators(input,oldline,parsed);
00132 find_branches(input);
00133 this->setMaps(&p_busMap, &p_branchMap);
00134 find_transformer(input);
00135 find_area(input);
00136 find_2term(input);
00137 find_shunt(input);
00138 #if 0
00139 find_imped_corr(input);
00140 find_multi_term(input);
00141 find_multi_section(input);
00142 find_zone(input);
00143 find_interarea(input);
00144 find_owner(input);
00145
00146 #endif
00147 #if 0
00148
00149 int i;
00150 printf("BUS data size: %d\n",(int)p_busData.size());
00151 for (i=0; i<p_busData.size(); i++) {
00152 printf("Dumping bus: %d\n",i);
00153 p_busData[i]->dump();
00154 }
00155 printf("BRANCH data size: %d\n",(int)p_branchData.size());
00156 for (i=0; i<p_branchData.size(); i++) {
00157 printf("Dumping branch: %d\n",i);
00158 p_branchData[i]->dump();
00159 }
00160 #endif
00161 input.close();
00162 }
00163 MPI_Comm comm = static_cast<MPI_Comm>(p_network->communicator());
00164 int ierr;
00165
00166 int isval, irval;
00167 if (me == 0) {
00168 isval = p_case_id;
00169 } else {
00170 isval = 0;
00171 }
00172 ierr = MPI_Allreduce(&isval,&irval,1,MPI_INT,MPI_SUM,comm);
00173 p_case_id = irval;
00174 double sval, rval;
00175 if (me == 0) {
00176 sval = p_case_sbase;
00177 } else {
00178 sval = 0.0;
00179 }
00180 ierr = MPI_Allreduce(&sval,&rval,1,MPI_DOUBLE,MPI_SUM,comm);
00181 p_case_sbase = rval;
00182 p_timer->stop(t_case);
00183 this->setCaseID(p_case_id);
00184 this->setCaseSBase(p_case_sbase);
00185 p_network->broadcastNetworkData(0);
00186 p_network_data = p_network->getNetworkData();
00187 }
00188
00189 void find_case(std::ifstream & input)
00190 {
00191
00192 std::string line;
00193
00194
00195
00196
00197 std::getline(input, line);
00198 while (check_comment(line)) {
00199 std::getline(input, line);
00200 }
00201 std::vector<std::string> split_line;
00202
00203 this->cleanComment(line);
00204 boost::algorithm::split(split_line, line, boost::algorithm::is_any_of(" "), boost::token_compress_on);
00205
00206
00207 p_case_id = atoi(split_line[0].c_str());
00208
00209
00210 p_case_sbase = atof(split_line[1].c_str());
00211
00212 p_network_data->addValue(CASE_SBASE, p_case_sbase);
00213 p_network_data->addValue(CASE_ID, p_case_id);
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228 }
00229
00230 void find_buses(std::ifstream & input)
00231 {
00232 std::string line;
00233 int index = 0;
00234 int o_idx;
00235 std::getline(input, line);
00236 std::getline(input, line);
00237 std::getline(input, line);
00238 double pl,ql,bl,gl;
00239
00240 while(test_end(line)) {
00241 std::vector<std::string> split_line;
00242 this->cleanComment(line);
00243 boost::split(split_line, line, boost::algorithm::is_any_of(","),
00244 boost::token_compress_on);
00245 boost::shared_ptr<gridpack::component::DataCollection>
00246 data(new gridpack::component::DataCollection);
00247 int nstr = split_line.size();
00248
00249
00250 o_idx = atoi(split_line[0].c_str());
00251 data->addValue(BUS_NUMBER, o_idx);
00252 p_busData.push_back(data);
00253 p_busMap.insert(std::pair<int,int>(o_idx,index));
00254
00255
00256 data->addValue(CASE_SBASE, p_case_sbase);
00257 data->addValue(CASE_ID, p_case_id);
00258
00259
00260 if (nstr > 9) data->addValue(BUS_NAME, split_line[9].c_str());
00261
00262
00263 if (nstr > 10) data->addValue(BUS_BASEKV, atof(split_line[10].c_str()));
00264
00265
00266 if (nstr > 1) data->addValue(BUS_TYPE, atoi(split_line[1].c_str()));
00267
00268
00269 gl = 0.0;
00270 if (nstr > 4) {
00271 gl = atof(split_line[4].c_str());
00272 }
00273
00274
00275 bl = 0.0;
00276 if (nstr > 5) {
00277 bl = atof(split_line[5].c_str());
00278 }
00279 if (gl != 0.0 || bl != 0.0) {
00280 data->addValue(BUS_SHUNT_GL, atof(split_line[4].c_str()));
00281 data->addValue(BUS_SHUNT_GL, atof(split_line[4].c_str()),0);
00282 data->addValue(BUS_SHUNT_BL, atof(split_line[5].c_str()));
00283 data->addValue(BUS_SHUNT_BL, atof(split_line[5].c_str()),0);
00284 data->addValue(SHUNT_BUSNUMBER,o_idx);
00285 int ival = 1;
00286 data->addValue(SHUNT_NUMBER,ival);
00287 data->addValue(SHUNT_ID, "1 ", 0);
00288 }
00289
00290
00291 if (nstr > 11) data->addValue(BUS_ZONE, atoi(split_line[11].c_str()));
00292
00293
00294 if (nstr > 6) data->addValue(BUS_AREA, atoi(split_line[6].c_str()));
00295
00296
00297 if (nstr > 7) data->addValue(BUS_VOLTAGE_MAG, atof(split_line[7].c_str()));
00298
00299
00300 if (nstr > 8) data->addValue(BUS_VOLTAGE_ANG, atof(split_line[8].c_str()));
00301
00302
00303 if (nstr > 6) data->addValue(BUS_OWNER, atoi(split_line[6].c_str()));
00304
00305
00306 pl = 0.0;
00307 if (nstr > 2) {
00308 pl = atof(split_line[2].c_str());
00309 }
00310
00311
00312 ql = 0.0;
00313 if (nstr > 3) {
00314 ql = atof(split_line[3].c_str());
00315 }
00316 if (pl != 0.0 || ql != 0.0) {
00317 data->addValue(LOAD_PL, atof(split_line[2].c_str()));
00318 data->addValue(LOAD_PL, atof(split_line[2].c_str()),0);
00319 std::string tmp("1 ");
00320 data->addValue(LOAD_ID,tmp.c_str(),0);
00321 data->addValue(LOAD_QL, atof(split_line[3].c_str()));
00322 data->addValue(LOAD_QL, atof(split_line[3].c_str()),0);
00323 int ival = 1;
00324 data->addValue(LOAD_NUMBER,ival);
00325 data->addValue(LOAD_STATUS,ival,0);
00326 data->addValue(LOAD_BUSNUMBER,o_idx);
00327 }
00328
00329 index++;
00330 std::getline(input, line);
00331 }
00332 }
00333
00334 void find_generators(std::ifstream & input, std::string &oldline, bool &parsed)
00335 {
00336 std::string line;
00337 if (parsed) {
00338 std::getline(input, line);
00339 } else {
00340 line = oldline;
00341 }
00342 while(test_end(line)) {
00343 std::vector<std::string> split_line;
00344 this->cleanComment(line);
00345 boost::split(split_line, line, boost::algorithm::is_any_of(","),
00346 boost::token_compress_on);
00347
00348
00349 int l_idx, o_idx;
00350 o_idx = atoi(split_line[0].c_str());
00351 std::map<int, int>::iterator it;
00352 int nstr = split_line.size();
00353 it = p_busMap.find(o_idx);
00354 if (it != p_busMap.end()) {
00355 l_idx = it->second;
00356 } else {
00357 std::getline(input, line);
00358 continue;
00359 }
00360
00361
00362 int ngen;
00363 if (!p_busData[l_idx]->getValue(GENERATOR_NUMBER, &ngen)) ngen = 0;
00364
00365
00366 p_busData[l_idx]->addValue(GENERATOR_BUSNUMBER, atoi(split_line[0].c_str()), ngen);
00367
00368
00369 gridpack::utility::StringUtils util;
00370 std::string tag = util.clean2Char(split_line[1]);
00371
00372 p_busData[l_idx]->addValue(GENERATOR_ID, tag.c_str(), ngen);
00373
00374
00375 if (nstr > 2) p_busData[l_idx]->addValue(GENERATOR_PG, atof(split_line[2].c_str()),
00376 ngen);
00377
00378
00379 if (nstr > 3) p_busData[l_idx]->addValue(GENERATOR_QG, atof(split_line[3].c_str()),
00380 ngen);
00381
00382
00383 if (nstr > 4) p_busData[l_idx]->addValue(GENERATOR_QMAX,
00384 atof(split_line[4].c_str()), ngen);
00385
00386
00387 if (nstr > 5) p_busData[l_idx]->addValue(GENERATOR_QMIN,
00388 atof(split_line[5].c_str()), ngen);
00389
00390
00391 if (nstr > 6) p_busData[l_idx]->addValue(GENERATOR_VS, atof(split_line[6].c_str()),
00392 ngen);
00393
00394
00395 if (nstr > 7) p_busData[l_idx]->addValue(GENERATOR_IREG,
00396 atoi(split_line[7].c_str()), ngen);
00397
00398
00399 if (nstr > 8) p_busData[l_idx]->addValue(GENERATOR_MBASE,
00400 atof(split_line[8].c_str()), ngen);
00401
00402
00403 if (nstr > 9) p_busData[l_idx]->addValue(GENERATOR_ZSOURCE,
00404 gridpack::ComplexType(atof(split_line[9].c_str()),
00405 atof(split_line[10].c_str())), ngen);
00406
00407
00408 if (nstr > 11) p_busData[l_idx]->addValue(GENERATOR_XTRAN,
00409 gridpack::ComplexType(atof(split_line[11].c_str()),
00410 atof(split_line[12].c_str())), ngen);
00411
00412
00413 if (nstr > 11) p_busData[l_idx]->addValue(GENERATOR_RT, atof(split_line[11].c_str()),
00414 ngen);
00415
00416
00417 if (nstr > 12) p_busData[l_idx]->addValue(GENERATOR_XT, atof(split_line[12].c_str()),
00418 ngen);
00419
00420
00421 if (nstr > 13) p_busData[l_idx]->addValue(GENERATOR_GTAP,
00422 atof(split_line[13].c_str()), ngen);
00423
00424
00425 if (nstr > 14) p_busData[l_idx]->addValue(GENERATOR_STAT,
00426 atoi(split_line[14].c_str()), ngen);
00427
00428
00429 if (nstr > 15) p_busData[l_idx]->addValue(GENERATOR_RMPCT,
00430 atof(split_line[15].c_str()), ngen);
00431
00432
00433 if (nstr > 16) p_busData[l_idx]->addValue(GENERATOR_PMAX,
00434 atof(split_line[16].c_str()), ngen);
00435
00436
00437 if (nstr > 17) p_busData[l_idx]->addValue(GENERATOR_PMIN,
00438 atof(split_line[17].c_str()), ngen);
00439
00440
00441
00442 if (nstr > 18) p_busData[l_idx]->addValue(GENERATOR_REACTANCE,
00443 atof(split_line[18].c_str()), ngen);
00444
00445
00446 if (nstr > 19) p_busData[l_idx]->addValue(GENERATOR_RESISTANCE,
00447 atof(split_line[19].c_str()), ngen);
00448
00449
00450 if (nstr > 20) p_busData[l_idx]->addValue(GENERATOR_TRANSIENT_REACTANCE,
00451 atof(split_line[20].c_str()), ngen);
00452
00453
00454 if (nstr > 21) p_busData[l_idx]->addValue(GENERATOR_SUBTRANSIENT_REACTANCE,
00455 atof(split_line[21].c_str()), ngen);
00456
00457
00458
00459 if (nstr > 22) p_busData[l_idx]->addValue(GENERATOR_INERTIA_CONSTANT_H,
00460 atof(split_line[22].c_str()), ngen);
00461
00462
00463 if (nstr > 23) p_busData[l_idx]->addValue(GENERATOR_DAMPING_COEFFICIENT_0,
00464 atof(split_line[23].c_str()), ngen);
00465
00466
00467 if (ngen == 0) {
00468 ngen = 1;
00469 p_busData[l_idx]->addValue(GENERATOR_NUMBER,ngen);
00470 } else {
00471 ngen++;
00472 p_busData[l_idx]->setValue(GENERATOR_NUMBER,ngen);
00473 }
00474
00475 std::getline(input, line);
00476 }
00477 }
00478
00479 void find_branches(std::ifstream & input)
00480 {
00481 std::string line;
00482 int o_idx1, o_idx2;
00483 int index = 0;
00484
00485 std::getline(input, line);
00486
00487 int nelems;
00488 while(test_end(line)) {
00489 std::pair<int, int> branch_pair;
00490 std::vector<std::string> split_line;
00491 this->cleanComment(line);
00492 boost::split(split_line, line, boost::algorithm::is_any_of(","),
00493 boost::token_compress_on);
00494
00495 o_idx1 = atoi(split_line[0].c_str());
00496 o_idx2 = atoi(split_line[1].c_str());
00497
00498
00499 if (o_idx1 < 0) o_idx1 = -o_idx1;
00500 if (o_idx2 < 0) o_idx2 = -o_idx2;
00501
00502
00503 int l_idx = 0;
00504 branch_pair = std::pair<int,int>(o_idx1, o_idx2);
00505 std::map<std::pair<int, int>, int>::iterator it;
00506 it = p_branchMap.find(branch_pair);
00507
00508 bool switched = false;
00509 if (it != p_branchMap.end()) {
00510 l_idx = it->second;
00511 p_branchData[l_idx]->getValue(BRANCH_NUM_ELEMENTS,&nelems);
00512 } else {
00513
00514 std::pair<int, int> new_branch_pair;
00515 new_branch_pair = std::pair<int,int>(o_idx2, o_idx1);
00516 it = p_branchMap.find(new_branch_pair);
00517 if (it != p_branchMap.end()) {
00518
00519
00520 l_idx = it->second;
00521 p_branchData[l_idx]->getValue(BRANCH_NUM_ELEMENTS,&nelems);
00522 switched = true;
00523 } else {
00524 boost::shared_ptr<gridpack::component::DataCollection>
00525 data(new gridpack::component::DataCollection);
00526 l_idx = p_branchData.size();
00527 p_branchData.push_back(data);
00528 nelems = 0;
00529 p_branchData[l_idx]->addValue(BRANCH_NUM_ELEMENTS,nelems);
00530 }
00531 }
00532
00533 if (nelems == 0) {
00534
00535 p_branchData[l_idx]->addValue(BRANCH_INDEX, index);
00536
00537
00538 p_branchData[l_idx]->addValue(BRANCH_FROMBUS, o_idx1);
00539
00540
00541 p_branchData[l_idx]->addValue(BRANCH_TOBUS, o_idx2);
00542
00543
00544 p_branchMap.insert(std::pair<std::pair<int, int>, int >(branch_pair,
00545 index));
00546 index++;
00547 }
00548
00549
00550 p_branchData[l_idx]->addValue(BRANCH_SWITCHED, switched, nelems);
00551
00552
00553 gridpack::utility::StringUtils util;
00554 std::string tag = util.clean2Char(split_line[2]);
00555
00556 p_branchData[l_idx]->addValue(BRANCH_CKT, tag.c_str(),
00557 nelems);
00558
00559
00560 p_branchData[l_idx]->addValue(BRANCH_R, atof(split_line[3].c_str()),
00561 nelems);
00562
00563
00564 p_branchData[l_idx]->addValue(BRANCH_X, atof(split_line[4].c_str()),
00565 nelems);
00566
00567
00568 p_branchData[l_idx]->addValue(BRANCH_B, atof(split_line[5].c_str()),
00569 nelems);
00570
00571
00572 p_branchData[l_idx]->addValue(BRANCH_RATING_A,
00573 atof(split_line[6].c_str()), nelems);
00574
00575
00576 p_branchData[l_idx]->addValue(BRANCH_RATING_B,
00577 atof(split_line[7].c_str()), nelems);
00578
00579
00580 p_branchData[l_idx]->addValue(BRANCH_RATING_C,
00581 atof(split_line[8].c_str()), nelems);
00582
00583
00584 p_branchData[l_idx]->addValue(BRANCH_TAP, atof(split_line[9].c_str()), nelems);
00585
00586
00587 p_branchData[l_idx]->addValue(BRANCH_SHIFT,
00588 atof(split_line[10].c_str()), nelems);
00589
00590
00591 p_branchData[l_idx]->addValue(BRANCH_SHUNT_ADMTTNC_G1,
00592 atof(split_line[11].c_str()), nelems);
00593
00594
00595 p_branchData[l_idx]->addValue(BRANCH_SHUNT_ADMTTNC_B1,
00596 atof(split_line[12].c_str()), nelems);
00597
00598
00599 p_branchData[l_idx]->addValue(BRANCH_SHUNT_ADMTTNC_G2,
00600 atof(split_line[13].c_str()), nelems);
00601
00602
00603 p_branchData[l_idx]->addValue(BRANCH_SHUNT_ADMTTNC_B2,
00604 atof(split_line[14].c_str()), nelems);
00605
00606
00607 p_branchData[l_idx]->addValue(BRANCH_STATUS,
00608 atoi(split_line[15].c_str()), nelems);
00609
00610 nelems++;
00611 p_branchData[l_idx]->setValue(BRANCH_NUM_ELEMENTS,nelems);
00612 std::getline(input, line);
00613 }
00614 }
00615
00616
00617
00618
00619 void find_transformer(std::ifstream & input)
00620 {
00621 std::string line;
00622
00623 std::getline(input, line);
00624
00625 std::pair<int, int> branch_pair;
00626
00627
00628
00629 while(test_end(line)) {
00630 std::vector<std::string> split_line;
00631 this->cleanComment(line);
00632 boost::split(split_line, line, boost::algorithm::is_any_of(","),
00633 boost::token_compress_on);
00634
00635
00636 int fromBus = atoi(split_line[0].c_str());
00637 if (fromBus < 0) fromBus = -fromBus;
00638
00639
00640 int toBus = atoi(split_line[1].c_str());
00641 if (toBus < 0) toBus = -toBus;
00642
00643
00644 int l_idx = 0;
00645 branch_pair = std::pair<int,int>(fromBus, toBus);
00646 std::map<std::pair<int, int>, int>::iterator it;
00647 it = p_branchMap.find(branch_pair);
00648
00649 if (it != p_branchMap.end()) {
00650 l_idx = it->second;
00651 } else {
00652 std::getline(input, line);
00653 continue;
00654 }
00655
00656
00657
00658
00659 int nelems = 0;
00660 p_branchData[l_idx]->getValue(BRANCH_NUM_ELEMENTS,&nelems);
00661 gridpack::utility::StringUtils util;
00662 std::string b_ckt(util.clean2Char(split_line[2]));
00663 int i;
00664 int idx = -1;
00665 for (i=0; i<nelems; i++) {
00666 std::string t_ckt;
00667 p_branchData[l_idx]->getValue(BRANCH_CKT,&t_ckt,i);
00668 if (b_ckt == t_ckt) {
00669 idx = i;
00670 break;
00671 }
00672 }
00673 if (idx == -1) {
00674 printf("No match for transformer from %d to %d\n",
00675 fromBus,toBus);
00676 continue;
00677 }
00678
00679
00680
00681
00682
00683 p_branchData[l_idx]->addValue(TRANSFORMER_CONTROL,
00684 atoi(split_line[3].c_str()),idx);
00685
00686
00687
00688
00689
00690 p_branchData[l_idx]->addValue(TRANSFORMER_RMA,
00691 atof(split_line[4].c_str()),idx);
00692
00693
00694
00695
00696
00697 p_branchData[l_idx]->addValue(TRANSFORMER_RMI,
00698 atof(split_line[5].c_str()),idx);
00699
00700
00701
00702
00703
00704 p_branchData[l_idx]->addValue(TRANSFORMER_VMA,
00705 atof(split_line[6].c_str()),idx);
00706
00707
00708
00709
00710
00711 p_branchData[l_idx]->addValue(TRANSFORMER_VMI,
00712 atof(split_line[7].c_str()),idx);
00713
00714
00715
00716
00717
00718 p_branchData[l_idx]->addValue(TRANSFORMER_STEP,
00719 atof(split_line[8].c_str()),idx);
00720
00721
00722
00723
00724
00725 p_branchData[l_idx]->addValue(TRANSFORMER_TABLE,
00726 atof(split_line[9].c_str()),idx);
00727
00728
00729 #if 0
00730
00731
00732
00733
00734
00735 p_branchData[l_idx]->addValue(TRANSFORMER_BUS1, atoi(split_line[0].c_str()));
00736
00737
00738
00739
00740
00741 p_branchData[l_idx]->addValue(TRANSFORMER_BUS2, atoi(split_line[1].c_str()));
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753 p_branchData[l_idx]->addValue(TRANSFORMER_CKT, split_line[2].c_str());
00754
00755
00756
00757
00758
00759 p_branchData[l_idx]->addValue(TRANSFORMER_CW, atoi(split_line[3].c_str()));
00760
00761
00762
00763
00764
00765 p_branchData[l_idx]->addValue(TRANSFORMER_CZ, atoi(split_line[5].c_str()));
00766
00767
00768
00769
00770
00771 p_branchData[l_idx]->addValue(TRANSFORMER_CM, atoi(split_line[5].c_str()));
00772
00773
00774
00775
00776
00777 p_branchData[l_idx]->addValue(TRANSFORMER_MAG1, atof(split_line[5].c_str()));
00778
00779
00780
00781
00782
00783 p_branchData[l_idx]->addValue(TRANSFORMER_MAG2, atof(split_line[5].c_str()));
00784
00785
00786
00787
00788
00789 p_branchData[l_idx]->addValue(TRANSFORMER_NMETR, atoi(split_line[1].c_str()));
00790
00791
00792
00793
00794
00795 p_branchData[l_idx]->addValue(TRANSFORMER_NAME, split_line[2].c_str());
00796
00797
00798
00799
00800
00801
00802 p_branchData[l_idx]->addValue(TRANSFORMER_STATUS, atoi(split_line[1].c_str()));
00803
00804
00805
00806
00807
00808 p_branchData[l_idx]->addValue(TRANSFORMER_OWNER, atoi(split_line[1].c_str()));
00809
00810
00811
00812
00813
00814 p_branchData[l_idx]->addValue(TRANSFORMER_R1_2, atof(split_line[1].c_str()));
00815
00816
00817
00818
00819
00820 p_branchData[l_idx]->addValue(TRANSFORMER_X1_2, atof(split_line[1].c_str()));
00821
00822
00823
00824
00825
00826 p_branchData[l_idx]->addValue(TRANSFORMER_SBASE1_2, atof(split_line[1].c_str()));
00827 #endif
00828
00829 std::getline(input, line);
00830 }
00831 }
00832
00833 void find_area(std::ifstream & input)
00834 {
00835 std::string line;
00836
00837 std::getline(input, line);
00838
00839 while(test_end(line)) {
00840 std::vector<std::string> split_line;
00841 this->cleanComment(line);
00842 boost::split(split_line, line, boost::algorithm::is_any_of(","), boost::token_compress_on);
00843
00844
00845 int l_idx, o_idx;
00846 o_idx = atoi(split_line[1].c_str());
00847 std::map<int, int>::iterator it;
00848 it = p_busMap.find(o_idx);
00849 if (it != p_busMap.end()) {
00850 l_idx = it->second;
00851 } else {
00852 std::getline(input, line);
00853 continue;
00854 }
00855 p_busData[l_idx]->addValue(AREAINTG_ISW, atoi(split_line[1].c_str()));
00856
00857
00858 p_busData[l_idx]->addValue(AREAINTG_NUMBER, atoi(split_line[0].c_str()));
00859
00860
00861 p_busData[l_idx]->addValue(AREAINTG_PDES, atof(split_line[2].c_str()));
00862
00863
00864 p_busData[l_idx]->addValue(AREAINTG_PTOL, atof(split_line[3].c_str()));
00865
00866
00867 p_busData[l_idx]->addValue(AREAINTG_NAME, split_line[4].c_str());
00868
00869 std::getline(input, line);
00870 }
00871 }
00872
00873 void find_2term(std::ifstream & input)
00874 {
00875 std::string line;
00876
00877 std::getline(input, line);
00878
00879 while(test_end(line)) {
00880 std::vector<std::string> split_line;
00881 this->cleanComment(line);
00882 boost::split(split_line, line, boost::algorithm::is_any_of(","), boost::token_compress_on);
00883 std::getline(input, line);
00884 }
00885 }
00886
00887 void find_line(std::ifstream & input)
00888 {
00889 std::string line;
00890
00891 std::getline(input, line);
00892
00893 while(test_end(line)) {
00894 std::vector<std::string> split_line;
00895 this->cleanComment(line);
00896 boost::split(split_line, line, boost::algorithm::is_any_of(","), boost::token_compress_on);
00897 std::getline(input, line);
00898 }
00899 }
00900
00901
00902
00903
00904
00905 void find_shunt(std::ifstream & input)
00906 {
00907 std::string line;
00908
00909 std::getline(input, line);
00910 while(test_end(line)) {
00911 std::vector<std::string> split_line;
00912 this->cleanComment(line);
00913 boost::split(split_line, line, boost::algorithm::is_any_of(","), boost::token_compress_on);
00914
00915
00916
00917
00918
00919 int l_idx, o_idx;
00920 l_idx = atoi(split_line[0].c_str());
00921 std::map<int, int>::iterator it;
00922 it = p_busMap.find(l_idx);
00923 if (it != p_busMap.end()) {
00924 o_idx = it->second;
00925 } else {
00926 std::getline(input, line);
00927 continue;
00928 }
00929 int nval = split_line.size();
00930
00931 p_busData[o_idx]->addValue(SWSHUNT_BUSNUMBER, atoi(split_line[0].c_str()));
00932
00933
00934
00935
00936
00937 p_busData[o_idx]->addValue(SHUNT_MODSW, atoi(split_line[1].c_str()));
00938
00939
00940
00941
00942
00943 p_busData[o_idx]->addValue(SHUNT_VSWHI, atof(split_line[2].c_str()));
00944
00945
00946
00947
00948
00949 p_busData[o_idx]->addValue(SHUNT_VSWLO, atof(split_line[3].c_str()));
00950
00951
00952
00953
00954
00955 p_busData[o_idx]->addValue(SHUNT_SWREM, atoi(split_line[4].c_str()));
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973 p_busData[o_idx]->addValue(SHUNT_BINIT, atof(split_line[5].c_str()));
00974
00975
00976
00977
00978
00979 p_busData[o_idx]->addValue(SHUNT_N1, atoi(split_line[6].c_str()));
00980
00981
00982
00983
00984
00985 if (8<nval)
00986 p_busData[o_idx]->addValue(SHUNT_N2, atoi(split_line[8].c_str()));
00987
00988
00989
00990
00991
00992 if (10<nval)
00993 p_busData[o_idx]->addValue(SHUNT_N3, atoi(split_line[10].c_str()));
00994
00995
00996
00997
00998
00999 if (12<nval)
01000 p_busData[o_idx]->addValue(SHUNT_N4, atoi(split_line[12].c_str()));
01001
01002
01003
01004
01005
01006 if (14<nval)
01007 p_busData[o_idx]->addValue(SHUNT_N5, atoi(split_line[14].c_str()));
01008
01009
01010
01011
01012
01013 if (16<nval)
01014 p_busData[o_idx]->addValue(SHUNT_N6, atoi(split_line[16].c_str()));
01015
01016
01017
01018
01019
01020 if (18<nval)
01021 p_busData[o_idx]->addValue(SHUNT_N7, atoi(split_line[18].c_str()));
01022
01023
01024
01025
01026
01027 if (20<nval)
01028 p_busData[o_idx]->addValue(SHUNT_N8, atoi(split_line[20].c_str()));
01029
01030
01031
01032
01033
01034 if (7<nval)
01035 p_busData[o_idx]->addValue(SHUNT_B1, atof(split_line[7].c_str()));
01036
01037
01038
01039
01040
01041 if (9<nval)
01042 p_busData[o_idx]->addValue(SHUNT_B2, atof(split_line[9].c_str()));
01043
01044
01045
01046
01047
01048 if (11<nval)
01049 p_busData[o_idx]->addValue(SHUNT_B3, atof(split_line[11].c_str()));
01050
01051
01052
01053
01054
01055 if (13<nval)
01056 p_busData[o_idx]->addValue(SHUNT_B4, atof(split_line[13].c_str()));
01057
01058
01059
01060
01061
01062 if (15<nval)
01063 p_busData[o_idx]->addValue(SHUNT_B5, atof(split_line[15].c_str()));
01064
01065
01066
01067
01068
01069 if (17<nval)
01070 p_busData[o_idx]->addValue(SHUNT_B6, atof(split_line[17].c_str()));
01071
01072
01073
01074
01075
01076 if (19<nval)
01077 p_busData[o_idx]->addValue(SHUNT_B7, atof(split_line[19].c_str()));
01078
01079
01080
01081
01082
01083 if (21<nval)
01084 p_busData[o_idx]->addValue(SHUNT_B8, atof(split_line[21].c_str()));
01085
01086 std::getline(input, line);
01087 }
01088 }
01089
01090 void find_imped_corr(std::ifstream & input)
01091 {
01092 std::string line;
01093
01094 std::getline(input, line);
01095
01096 while(test_end(line)) {
01097 std::vector<std::string> split_line;
01098 this->cleanComment(line);
01099 boost::split(split_line, line, boost::algorithm::is_any_of(","), boost::token_compress_on);
01100 #if 0
01101 std::vector<gridpack::component::DataCollection> imped_corr_instance;
01102 gridpack::component::DataCollection data;
01103
01104
01105
01106
01107
01108 data.addValue(XFMR_CORR_TABLE_NUMBER, atoi(split_line[0].c_str()));
01109 imped_corr_instance.push_back(data);
01110
01111
01112
01113
01114
01115 data.addValue(XFMR_CORR_TABLE_Ti, atoi(split_line[0].c_str()));
01116 imped_corr_instance.push_back(data);
01117
01118
01119
01120
01121
01122 data.addValue(XFMR_CORR_TABLE_Fi, atoi(split_line[0].c_str()));
01123 imped_corr_instance.push_back(data);
01124
01125 imped_corr_set.push_back(imped_corr_instance);
01126 #endif
01127 std::getline(input, line);
01128 }
01129 }
01130
01131 void find_multi_section(std::ifstream & input)
01132 {
01133 std::string line;
01134
01135 std::getline(input, line);
01136
01137 while(test_end(line)) {
01138 #if 0
01139 std::vector<std::string> split_line;
01140 this->cleanComment(line);
01141 boost::split(split_line, line, boost::algorithm::is_any_of(","), boost::token_compress_on);
01142 std::vector<gridpack::component::DataCollection> multi_section_instance;
01143 gridpack::component::DataCollection data;
01144
01145
01146
01147
01148
01149
01150 data.addValue(MULTI_SEC_LINE_FROMBUS, atoi(split_line[0].c_str()));
01151 multi_section_instance.push_back(data);
01152
01153
01154
01155
01156
01157
01158 data.addValue(MULTI_SEC_LINE_TOBUS, atoi(split_line[0].c_str()));
01159 multi_section_instance.push_back(data);
01160
01161
01162
01163
01164
01165
01166 data.addValue(MULTI_SEC_LINE_ID, split_line[0].c_str());
01167 multi_section_instance.push_back(data);
01168
01169
01170
01171
01172
01173 data.addValue(MULTI_SEC_LINE_DUMi, atoi(split_line[0].c_str()));
01174 multi_section_instance.push_back(data);
01175
01176 multi_section.push_back(multi_section_instance);
01177 #endif
01178 std::getline(input, line);
01179 }
01180 }
01181
01182 void find_multi_term(std::ifstream & input)
01183 {
01184 std::string line;
01185
01186 std::getline(input, line);
01187
01188 while(test_end(line)) {
01189
01190 std::getline(input, line);
01191 }
01192 }
01193
01194
01195
01196
01197 void find_zone(std::ifstream & input)
01198 {
01199 std::string line;
01200
01201 std::getline(input, line);
01202 while(test_end(line)) {
01203
01204 std::getline(input, line);
01205 }
01206 }
01207
01208 void find_interarea(std::ifstream & input)
01209 {
01210 std::string line;
01211
01212 std::getline(input, line);
01213
01214 while(test_end(line)) {
01215 #if 0
01216 std::vector<std::string> split_line;
01217 this->cleanComment(line);
01218 boost::split(split_line, line, boost::algorithm::is_any_of(","), boost::token_compress_on);
01219 std::vector<gridpack::component::DataCollection> inter_area_instance;
01220 gridpack::component::DataCollection data;
01221
01222
01223
01224
01225
01226 data.addValue(INTERAREA_TRANSFER_FROM, atoi(split_line[0].c_str()));
01227 inter_area_instance.push_back(data);
01228
01229
01230
01231
01232
01233 data.addValue(INTERAREA_TRANSFER_TO, atoi(split_line[0].c_str()));
01234 inter_area_instance.push_back(data);
01235
01236
01237
01238
01239
01240 data.addValue(INTERAREA_TRANSFER_TRID, split_line[0].c_str()[0]);
01241 inter_area_instance.push_back(data);
01242
01243
01244
01245
01246
01247 data.addValue(INTERAREA_TRANSFER_PTRAN, atof(split_line[0].c_str()));
01248 inter_area_instance.push_back(data);
01249
01250 inter_area.push_back(inter_area_instance);
01251 #endif
01252 std::getline(input, line);
01253 }
01254 }
01255
01256
01257
01258
01259
01260
01261
01262
01263 void find_owner(std::ifstream & input)
01264 {
01265 std::string line;
01266 std::getline(input, line);
01267
01268 while(test_end(line)) {
01269 #if 0
01270 std::vector<std::string> split_line;
01271 this->cleanComment(line);
01272 boost::split(split_line, line, boost::algorithm::is_any_of(","), boost::token_compress_on);
01273 std::vector<gridpack::component::DataCollection> owner_instance;
01274 gridpack::component::DataCollection data;
01275
01276 data.addValue(OWNER_NUMBER, atoi(split_line[0].c_str()));
01277 owner_instance.push_back(data);
01278
01279 data.addValue(OWNER_NAME, split_line[1].c_str());
01280 owner_instance.push_back(data);
01281
01282 owner.push_back(owner_instance);
01283 #endif
01284 std::getline(input, line);
01285 }
01286 }
01287
01288
01289 void brdcst_data(void)
01290 {
01291 int t_brdcst = p_timer->createCategory("Parser:brdcst_data");
01292 int t_serial = p_timer->createCategory("Parser:data packing and unpacking");
01293 MPI_Comm comm = static_cast<MPI_Comm>(p_network->communicator());
01294 int me(p_network->communicator().rank());
01295 int nprocs(p_network->communicator().size());
01296 if (nprocs == 1) return;
01297 p_timer->start(t_brdcst);
01298
01299
01300
01301 int sbus, sbranch;
01302 if (me == 0) {
01303 sbus = p_busData.size();
01304 sbranch = p_branchData.size();
01305 } else {
01306 sbus = 0;
01307 sbranch = 0;
01308 }
01309 int ierr, nbus, nbranch;
01310 ierr = MPI_Allreduce(&sbus, &nbus, 1, MPI_INT, MPI_SUM, comm);
01311 ierr = MPI_Allreduce(&sbranch, &nbranch, 1, MPI_INT, MPI_SUM, comm);
01312 double rprocs = static_cast<double>(nprocs);
01313 double rme = static_cast<double>(me);
01314 int n, i;
01315 std::vector<gridpack::component::DataCollection>recvV;
01316
01317 if (me == 0) {
01318 for (n=0; n<nprocs; n++) {
01319 double rn = static_cast<double>(n);
01320 int istart = static_cast<int>(static_cast<double>(nbus)*rn/rprocs);
01321 int iend = static_cast<int>(static_cast<double>(nbus)*(rn+1.0)/rprocs);
01322 if (n != 0) {
01323 p_timer->start(t_serial);
01324 std::vector<gridpack::component::DataCollection> sendV;
01325 for (i=istart; i<iend; i++) {
01326 sendV.push_back(*(p_busData[i]));
01327 }
01328 p_timer->stop(t_serial);
01329 static_cast<boost::mpi::communicator>(p_network->communicator()).send(n,n,sendV);
01330 } else {
01331 p_timer->start(t_serial);
01332 for (i=istart; i<iend; i++) {
01333 recvV.push_back(*(p_busData[i]));
01334 }
01335 p_timer->stop(t_serial);
01336 }
01337 }
01338 } else {
01339 int istart = static_cast<int>(static_cast<double>(nbus)*rme/rprocs);
01340 int iend = static_cast<int>(static_cast<double>(nbus)*(rme+1.0)/rprocs)-1;
01341 static_cast<boost::mpi::communicator>(p_network->communicator()).recv(0,me,recvV);
01342 }
01343 int nsize = recvV.size();
01344 p_busData.clear();
01345 p_timer->start(t_serial);
01346 for (i=0; i<nsize; i++) {
01347 boost::shared_ptr<gridpack::component::DataCollection> data(new
01348 gridpack::component::DataCollection);
01349 *data = recvV[i];
01350 p_busData.push_back(data);
01351 }
01352 p_timer->stop(t_serial);
01353 recvV.clear();
01354
01355 if (me == 0) {
01356 for (n=0; n<nprocs; n++) {
01357 double rn = static_cast<double>(n);
01358 int istart = static_cast<int>(static_cast<double>(nbranch)*rn/rprocs);
01359 int iend = static_cast<int>(static_cast<double>(nbranch)*(rn+1.0)/rprocs);
01360 if (n != 0) {
01361 p_timer->start(t_serial);
01362 std::vector<gridpack::component::DataCollection> sendV;
01363 for (i=istart; i<iend; i++) {
01364 sendV.push_back(*(p_branchData[i]));
01365 }
01366 p_timer->stop(t_serial);
01367 static_cast<boost::mpi::communicator>(p_network->communicator()).send(n,n,sendV);
01368 } else {
01369 p_timer->start(t_serial);
01370 for (i=istart; i<iend; i++) {
01371 recvV.push_back(*(p_branchData[i]));
01372 }
01373 p_timer->stop(t_serial);
01374 }
01375 }
01376 } else {
01377 int istart = static_cast<int>(static_cast<double>(nbranch)*rme/rprocs);
01378 int iend = static_cast<int>(static_cast<double>(nbranch)*(rme+1.0)/rprocs)-1;
01379 static_cast<boost::mpi::communicator>(p_network->communicator()).recv(0,me,recvV);
01380 }
01381 nsize = recvV.size();
01382 p_branchData.clear();
01383 p_timer->start(t_serial);
01384 for (i=0; i<nsize; i++) {
01385 boost::shared_ptr<gridpack::component::DataCollection> data(new
01386 gridpack::component::DataCollection);
01387 *data = recvV[i];
01388 p_branchData.push_back(data);
01389 }
01390 p_timer->stop(t_serial);
01391 #if 0
01392
01393 printf("p[%d] BUS data size: %d\n",me,p_busData.size());
01394 for (i=0; i<p_busData.size(); i++) {
01395 printf("p[%d] Dumping bus: %d\n",me,i);
01396 p_busData[i]->dump();
01397 }
01398 printf("p[%d] BRANCH data size: %d\n",me,p_branchData.size());
01399 for (i=0; i<p_branchData.size(); i++) {
01400 printf("p[%d] Dumping branch: %d\n",me,i);
01401 p_branchData[i]->dump();
01402 }
01403 #endif
01404 p_timer->stop(t_brdcst);
01405 }
01406
01407 private:
01408
01409
01410
01411
01412 bool test_end(std::string &str) const
01413 {
01414 #if 1
01415 if (str[0] == TERM_CHAR) {
01416 return false;
01417 }
01418 int len = str.length();
01419 int i=0;
01420 while (i<len && str[i] == ' ') {
01421 i++;
01422 }
01423 if (i<len && str[i] != TERM_CHAR) {
01424 return true;
01425 } else if (i == len) {
01426 return true;
01427 } else if (str[i] == TERM_CHAR) {
01428 i++;
01429 if (i>=len || str[i] == ' ' || str[i] == '\\') {
01430 return false;
01431 } else {
01432 return true;
01433 }
01434 } else {
01435 return true;
01436 }
01437 #else
01438 if (str[0] == '0') {
01439 return false;
01440 } else {
01441 return true;
01442 }
01443 #endif
01444 }
01445
01446
01447
01448
01449
01450 bool check_comment(std::string &str) const
01451 {
01452 int ntok = str.find_first_not_of(' ',0);
01453 if (ntok != std::string::npos && ntok+1 != std::string::npos &&
01454 str[ntok] == '/' && str[ntok+1] == '/') {
01455 return true;
01456 } else {
01457 return false;
01458 }
01459 }
01460
01461
01462
01463
01464
01465
01466
01467
01468
01469
01470
01471
01472
01473
01474
01475
01476
01477
01478
01479
01480
01481
01482
01483
01484
01485
01486
01487
01488
01489
01490
01491 boost::shared_ptr<_network> p_network;
01492
01493
01494 std::vector<boost::shared_ptr<gridpack::component::DataCollection> > p_busData;
01495
01496 std::vector<boost::shared_ptr<gridpack::component::DataCollection> > p_branchData;
01497
01498 std::map<int,int> p_busMap;
01499
01500 std::map<std::pair<int, int>, int> p_branchMap;
01501
01502
01503 int p_case_id;
01504 double p_case_sbase;
01505 gridpack::utility::CoarseTimer *p_timer;
01506
01507
01508
01509 boost::shared_ptr<gridpack::component::DataCollection> p_network_data;
01510 };
01511
01512 }
01513 }
01514 #endif